import {Component, EventEmitter, Input, Output} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {AppUrl} from "../../../app/app.general";

/**
 * Generated class for the FoodMenuPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-food-menu',
  templateUrl: 'food-menu.html',
})
export class FoodMenuPage {

  private Foods: any;
  private image = {//图片请求数据绑定
    imagePrefix: AppUrl.image.getImage,
    type: {
      restaurant: AppUrl.image.type.restaurant,
      food: AppUrl.image.type.food
    },
    imageById: AppUrl.image.getById
  };
  private basket = {//购物车信息
    allPrice: 0,
    foodsList: []
  };
  private shoppingState = false;

  private chooseType;
  private selectedIndex = 0;
  @Input()
  private header: boolean = false;
  @Output() output = new EventEmitter<any>();
  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }

  @Input()
  set data(val: any) {
    this.Foods = val;
    if (this.Foods.fTypes)
      this.chooseType = this.Foods.fTypes[0].fType;
    console.log(this.Foods);
    console.log(this.chooseType);
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad FoodMenuPage');
  }

  /**
   *
   * @param i  数组index
   * @param type  类别
   *        例  炒饭 盖饭
   */
  showGoods(i, type) {
    console.log(i);
    console.log(type);
    this.selectedIndex = i;
    document.getElementById('#ID' + i).scrollIntoView(true);
  }
//添加商品到购物车
  addGood(food) {
    food.fNum++;
    // food.sumPrice = food.sumPrice + food.goodprice;
    this.basket.allPrice = this.basket.allPrice + food.price;
    let temp = this.basket.foodsList.filter(f => f.fName == food.fName);
    if (temp.length == 0) {
      this.basket.foodsList.push(food);
    }
  }

  //去除购物车商品
  reduceGood(food, i) {
    if (food.fNum > 0) {
      food.fNum--;
      // food.sumPrice = food.sumPrice - food.goodprice;
      this.basket.allPrice = this.basket.allPrice - food.price;
    }
    if (food.fNum == 0) {
      let temp = this.basket.foodsList.filter(f => f.fName == food.fName);
      if (temp.length > 0) {
        this.basket.foodsList.splice(i, 1);
      }
    }
  }

  //清空购物车商品
  cleanAll() {
    this.basket.allPrice = 0;
    for (let type of this.Foods.fTypes) {
      for (let food of type.foods) {
        food.fNum = 0;
      }
      this.basket.foodsList.pop();
    }
  }

  changeShoppingState() {
    this.shoppingState = !this.shoppingState;
  }
  outputBasket(){
    console.log(this.basket);
    this.output.emit(this.basket);
  }
}
